Common Issues When Using Microsoft.Office.Interop.Excel in .NET Projects
TLDR
dotnet publishdoes not supportResolveComReference. It is recommended to isolate Excel conversion functionality into a separate .NET Framework Web API service.- If you encounter an
0x80070005Access Denied error, you must configure DCOM permissions viadcomcnfgto grant access to the IIS Application Pool account. - If an error occurs stating that Excel files cannot be found, you must manually create the
Desktopfolder in the system path to allow access by the COM component. - This solution is limited to Windows environments, and Microsoft Office must be installed on the server.
dotnet publish Compilation Issues
When this issue occurs: When using the dotnet publish command to compile code containing COM component references in .NET Core or .NET 5+ projects.
Since dotnet publish uses the MSBuild built into the .NET SDK, it does not support the ResolveComReference task, causing compilation to fail with the error message: error MSB4803: The .NET Core version of MSBuild does not support the task "ResolveComReference". Please use the .NET Framework version of MSBuild.
- Recommendation: Extract the Excel conversion logic and create a separate .NET Framework Web API project to handle the conversion, then have the main application call this API.
Runtime Error Handling Workflow
1. Insufficient DCOM Component Permissions
When this issue occurs: When an IIS application attempts to launch an Excel COM object, but the Application Pool account lacks sufficient DCOM access permissions.
If you encounter System.UnauthorizedAccessException: Retrieving the COM class factory... 80070005 Access Denied, please follow these steps to configure it:
- Open the DCOM configuration tool: Press Windows + R and enter
dcomcnfg. - Navigate to: Component Services > Computers > My Computer > DCOM Config.
- Locate "Microsoft Excel Application", right-click, and select "Properties".
- Switch to the "Security" tab and click "Customize" in the "Access Permissions" section.
- Add the corresponding
IIS APPPOOL\{Application Pool Name}to the user list. - Check the "Local Access" and "Remote Access" permissions.
2. Excel File Access Issues
When this issue occurs: In a Windows Server environment, when the Excel COM component starts, it attempts to access the system account's Desktop folder, but this folder does not exist by default.
If the program reports that it cannot find Excel-related files during execution, please manually create the Desktop folder in the corresponding path based on the installed Office bitness:
- 32-bit Office:
C:\Windows\SysWOW64\config\systemprofile\Desktop - 64-bit Office:
C:\Windows\System32\config\systemprofile\Desktop
Notes
- This configuration is only applicable to Windows environments.
- Microsoft Office software must be installed on the server.
- It is recommended to re-check the aforementioned permissions and folder settings after every Windows update.
Change Log
- 2024-10-21 Initial version created.
